/**
 * CREATE TABLE users (
 id INT PRIMARY KEY AUTO_INCREMENT,
 username VARCHAR(50) NOT NULL,
 email VARCHAR(100) UNIQUE NOT NULL,
 password VARCHAR(255) NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 is_active BOOLEAN DEFAULT TRUE
 );
 */

import { Field } from "../../Field";


export enum FieldType {
  Int = "INT",
  Varchar255 = "VARCHAR(255)",
}

export function createTable(name: string, fileds: Field[]) {
  let sql = `CREATE TABLE ${name} IF NOT EXISTS`;
  const filedInfos = fileds.map(f => generateFiledString(f));
  sql += ` ${filedInfos.join(" , ")}`
  return sql;
}

/**
 * 为一个字段生成一个filed
 * @param f
 */
export function generateFiledString(f: Field) {
  let str = `${f.name} ${f.type}`;
  if (f.primaryKey) {
    str += ` PRIMARY KEY AUTO_INCREMENT`
  }
  return str;
}